package com.app.utils.util.view.expression.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.utils.R;
import com.app.utils.util.ViewSizeUtils;
import com.app.utils.util.view.expression.bean.BiaoQin;

import java.util.List;

/**
 * 评论表情的适配器 每个item都是一张图片
 * 
 * @author xuxinghai
 * 
 */
public class BQAdapter extends BaseAdapter {

	public List<BiaoQin> list;
	public Context context;
	public LayoutInflater inflater;

	public BQAdapter(List<BiaoQin> list, Context context) {
		this.list = list;
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	public class Helper {
		ImageView imageView;
		TextView titleView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list != null && !list.isEmpty() ? list.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		BiaoQin bq = list.get(position);
		if(bq == null){
			return 1;
		}
		if(TextUtils.isEmpty(bq.getName())){
			return 2;
		}else{
			return 1;
		}
	}

	@SuppressWarnings("ResourceType")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Helper helper;
		if (convertView == null) {
			if(getItemViewType(position) == 2){
				//删除布局
				helper = new Helper();
				FrameLayout flayout = new FrameLayout(context);
				ImageView img = new ImageView(context);
				img.setScaleType(ImageView.ScaleType.CENTER);
				FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
						ViewGroup.LayoutParams.MATCH_PARENT,
						(int) ViewSizeUtils.getHeightSize(90)
						, Gravity.CENTER);
				img.setLayoutParams(layoutParams);
				img.setId(100);
				flayout.addView(img);
				convertView = flayout;
				helper.imageView = img;
				helper.imageView.setImageResource(list.get(position).getResourcesId());
				convertView.setTag(helper);
			}else{
				//表情布局
				helper = new Helper();
				convertView = inflater.inflate(R.layout.item_express,null);
				helper.imageView = (ImageView) convertView.findViewById(android.R.id.icon);
				helper.titleView = (TextView) convertView.findViewById(android.R.id.text1);
				convertView.setTag(helper);
				helper.imageView.setImageResource(list.get(position).getResourcesId());
				helper.titleView.setText(list.get(position).getName());
			}

		} else {
			if(getItemViewType(position) != 2){

				helper = (Helper) convertView.getTag();
				helper.imageView.setImageResource(list.get(position).getResourcesId());
				helper.titleView.setText(list.get(position).getName());
			}
		}

		return convertView;
	}

}
